<template>
  <div class="title">用户注册</div>
  <el-form :model="user" label-width="80px">
    <el-form-item label="用户名">
      <el-input v-model="user.name" />
    </el-form-item>
    <el-form-item label="密码">
      <el-input v-model="user.password" />
    </el-form-item>
    <el-form-item label="性别">
      <el-radio-group v-model="user.sex">
        <el-radio label="男" value="男" />
        <el-radio label="女" value="女" />
      </el-radio-group>
    </el-form-item>
    <el-form-item label="地区">
      <el-select v-model="user.addr" placeholder="请选择你的地区">
        <el-option label="中国大陆" value="CN" />
        <el-option label="中国香港" value="HK" />
        <el-option label="中国澳门" value="MO" />
        <el-option label="中国台湾" value="TW" />
      </el-select>
    </el-form-item>
    <el-form-item label="电话">
      <el-input v-model="user.tel" />
    </el-form-item>
    <el-form-item label="邮箱">
      <el-input v-model="user.email" />
    </el-form-item>
    <el-form-item label="爱好">
      <el-checkbox-group v-model="user.hobby">
        <el-checkbox label="唱" value="sing" name="sing" />
        <el-checkbox label="跳" value="dance" name="dance" />
        <el-checkbox label="Rap" value="rap" name="rap" />
        <el-checkbox label="篮球" value="basketball" name="basketball" />
      </el-checkbox-group>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="onSubmit">注册</el-button>
    </el-form-item>
  </el-form>
</template>

<script>

import { ElMessageBox } from 'element-plus'
import axios from 'axios'

export default {
  name: 'Register',
  data() {
    return {
      user: {
        name: '',
        password: '',
        sex: '',
        addr: '',
        tel: '',
        email: '',
        hobby: []
      }
    }
  },
  methods: {
    onSubmit() {

      axios.post('http://localhost:8081/register', JSON.stringify(this.user), {
        headers: {
          'Content-Type': 'application/json;charset=UTF-8',//设置请求头请求格式为JSON
        },
      }).then(
        (res) => {
          if (res.data.code == '200') {

            setTimeout(() => {
              ElMessageBox.alert('用户注册成功', res.data.msg, {
                confirmButtonText: 'OK',
              })
              this.$router.push({ path: '/Login' })
            }, 500);
          } else {
            setTimeout(() => {
              ElMessageBox.alert('用户注册失败', res.data.msg, {
                confirmButtonText: 'OK',
              })
            }, 500);
          }
        }
      )
    }

  }
}
</script>

<style>
.title {
  font-size: large;
  width: 100px;
  margin: 10px auto;
}
</style>
